using Front._1117_2;
using MySqlConnector;

namespace Front._1118;

public partial class Login : Form
{
    public Login()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(textBox1.Text))
        {
            textBox1.Text = "admin_super01";
        }
        if (string.IsNullOrEmpty(textBox2.Text))
        {
            textBox2.Text = "123456";
        }
        Console.OutputEncoding = System.Text.Encoding.UTF8;
        Console.InputEncoding = System.Text.Encoding.UTF8;
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=123456;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();
        
        string sql = "SELECT * FROM admin_info where admin_account = @admin_account and admin_password = @admin_password";
        
        using var command = new MySqlCommand(sql, connection);
        //获取文本框的值赋值给sql语句中的变量
        command.Parameters.AddWithValue("@admin_account", textBox1.Text);
        // 对密码进行md5加密
        string pwd = MD5Helper.GetMD5(textBox2.Text);
        command.Parameters.AddWithValue("@admin_password", pwd);
        
        using var reader = command.ExecuteReader();
        //判断是否有匹配的数据
        if (reader.HasRows)
        {
            MainForm mainForm = new MainForm();
            mainForm.Show();
            this.Hide();
        }
        else
        {
            MessageBox.Show("用户名或密码错误");
            textBox1.Text = "admin_super01";
            textBox2.Text = "123456";
        }
        
    }

    private void button2_Click(object sender, EventArgs e)
    {
        textBox1.Text = "admin_super01";
        textBox2.Text = "123456";
    }
}